package cn.snmp.mutual.common;

import java.util.LinkedList;

import cn.snmp.mutual.common.snmp.SnmpNioTarget;
import cn.snmp.mutual.common.snmp.SnmpNioTargetUtilities;
import cn.snmp.mutual.constants.NnTempLetError;
import lombok.extern.slf4j.Slf4j;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.handler.SnmpGetBulk;
import cn.snmp.mutual.handler.SnmpGetList;
import cn.snmp.mutual.handler.SnmpGetTable;
import cn.snmp.mutual.handler.SnmpGetTablePage;
import cn.snmp.mutual.handler.SnmpGetTableRow;
import cn.snmp.mutual.util.MibNodeDataUtils;
import cn.snmp.mutual.util.OperatorUtilities;

/**
 * @author dengkn
 * SNMP GET ,获取信息处理，（TABLE, LIST ,获取一条MIB,获取一页信息，获取全部信息）
 */
@Slf4j
@SuppressWarnings("all")
public class SnmpGetHandler {


    public SnmpNioTarget snmpGetHander(ObjService osCommandParams) {
        osCommandParams = MibNodeDataUtils.getMibTable(osCommandParams);
        ObjService osResult = osCommandParams.objectAt("MIB", 0);
        for (int i = 0; i < osResult.rowSize("MibTable"); i++) {
            if (osResult.objectAt("MibTable", i).getStoreValueMap().get("Name").equals((String) osCommandParams.getValue("TableName"))) {
                osResult = osResult.objectAt("MibTable", i);
            }
        }
        String strReInitSnmp = osResult.getStringValue("ReInitSnmp");
        osCommandParams.setValue("ReInitSnmp", strReInitSnmp);
        SnmpNioTarget snt = getSnmpTarget(osCommandParams);
        if (snt == null) {
            snt = new SnmpNioTarget(osCommandParams);
            snt.setFinishTag(true);
            osResult.setValue("isFinish", "true");
            osResult.setValue("ErrCode", NnTempLetError.INIT_SNMPTARGET_ERROR);
            osResult.setValue("ErrDesc", NnTempLetError.getErrorMessage(NnTempLetError.INIT_SNMPTARGET_ERROR));
            snt.setResult(osResult);
            return snt;
        }
        snt.setResult(osResult);
        snt.setCommunity(osCommandParams.getStringValue("ReadCommunity"));
        String strTableType = osResult.getStringValue("Type");
        String strInstance = getInstance(osCommandParams);
        String strPageSize = osCommandParams.getStringValue("RealPageSize");
        String strMaxRepetitions = osCommandParams.getStringValue("MaxRepetitions");
        if ("List".equalsIgnoreCase(strTableType)) {
			log.debug("I take List data.");
            SnmpGetList.hander(snt, osCommandParams, osResult);
        } else if ("Table".equalsIgnoreCase(strTableType)) {
            if (!"".equalsIgnoreCase(strMaxRepetitions)) {
				log.debug("get handler mutual data strMaxRepetitions not null");
				log.debug("I take SnmpGetBulk data.");
                SnmpGetBulk.hander(snt, osCommandParams, osResult);
            } else if (!"".equals(strInstance)) {
				log.debug("get handler mutual data strInstance not null");
				log.debug("I take one row data.");
                SnmpGetTableRow.hander(snt, osCommandParams, osResult);
            } else if (osCommandParams.objectSize("Instance") > 0) {
				log.debug("get handler mutual data Instance size > 0");
                ObjService osInstances = osCommandParams.objectAt("Instance", 0);
                LinkedList<String> vInstances = OperatorUtilities.shortInstance(osInstances);
                snt.setInstances((LinkedList<String>) vInstances.clone());
                SnmpGetTableRow.hander(snt, osCommandParams, osResult);
				log.debug("I take more row data.");
            } else if (strPageSize.equalsIgnoreCase("")) {
				log.debug("I take one table row data.");
                SnmpGetTable.hander(snt, osCommandParams, osResult);
            } else {
				log.debug("I don't know what I want, so I took a table data.");
                SnmpGetTablePage.hander(snt, osCommandParams, osResult);
            }
        } else {
            snt.setFinishTag(true);
            osResult.setValue("isFinish", "true");
            osResult.setValue("ErrCode", "" + NnTempLetError.TABLE_TYPE_ERROR);
            log.error(strTableType + "Not Found this mibTable type!");
            osResult.setValue("ErrDesc", "Unkonwn MibTable type [" + strTableType + "].");
        }
        return snt;
    }

    private static String getInstance(ObjService osCommandParams) {
        ObjService rowSet = osCommandParams.objectAt("RowSet", 0);
        String strInstance = osCommandParams.getStringValue("Instance");
        if (null == strInstance || "".equals(strInstance)) {
            if (rowSet == null) {
                log.error("get handler mutual data instance = null");
                return "";
            }
            String rowSetInstance = rowSet.getStringValue("Instance");
            if (null == rowSetInstance || "".equals(rowSetInstance)) {
                log.error("get handler mutual data instance = null");
                return "";
            } else {
                log.error("get handler mutual data instance =" + rowSetInstance);
                return rowSetInstance;
            }

        }
		log.debug("get handler mutual data instance =" + strInstance);
        return strInstance;

    }

    /**
     * @param snmpParam: snmp参数
     * @return 配置好的SnmpNioTarget信息
     */
    protected SnmpNioTarget getSnmpTarget(ObjService snmpParam) {
        SnmpNioTarget snmpTarget = null;
        try {
            snmpTarget = SnmpNioTargetUtilities.getSnmpNioTarget(snmpParam);
            SnmpNioTargetUtilities.initSnmpNioTarget(snmpTarget, snmpParam);
            return snmpTarget;
        } catch (Exception e) {
            log.error("get SnmpTarget error! Maybe SNMP parameter configuration error.", e);
            snmpTarget = null;
            return snmpTarget;
        }
    }
}
